package com.walking.slf4jmdc;

import com.walking.slf4jmdc.threadexecutor.DefaultThreadFactory;
import com.walking.slf4jmdc.threadexecutor.MDCThreadExecutor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author walking
 */
@SpringBootApplication
public class Slf4jMdcApplication {

    public static void main(String[] args) {
        SpringApplication.run(Slf4jMdcApplication.class, args);
    }
    @Bean
    public MDCThreadExecutor taskExecutor(){
        MDCThreadExecutor mdcThreadExecutor = new MDCThreadExecutor(new HashMap<String, String>() {{
            put("appId", "mdc-threadPool");
        }});
        mdcThreadExecutor.setCorePoolSize(10);
        mdcThreadExecutor.setMaxPoolSize(20);
        mdcThreadExecutor.setKeepAliveSeconds(5);
        mdcThreadExecutor.setQueueCapacity(50);
        mdcThreadExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        mdcThreadExecutor.setThreadFactory(new DefaultThreadFactory("walking-mdc"));

        return mdcThreadExecutor;
    }

}
